
import unittest

class TestSet(unittest.TestCase):
    
    def test_construct(self):
        aset = set("bookshop")
        self.assertEqual(["b","h","k","o","p","s"],list(aset))
        
    def test_update(self):
        aset = set("abc")
        ori_id = id(aset)
        
        aset.add("z")
        now_id = id(aset)
        
        self.assertEqual(["a","c","b","z"],list(aset))
        # shows that set is mutable, not copy-on-write
        self.assertEqual(ori_id,now_id)
        
    def test_in(self):
        aset = set("abc")
        self.assertTrue("a" in aset)
        self.assertTrue("z" not in aset)
        
    def test_equal(self):
        self.assertEqual(set("shop"),set("pohs"))
        
    def test_union(self):
        set1 = set("cheka")
        set2 = set("abc")
        self.assertEqual(set("chekab"),set1 | set2)
        
    def test_intersection(self):
        set1 = set("cheka")
        set2 = set("abc")
        self.assertEqual(set("ac"),set1 & set2)
        self.assertEqual(set("ac"),set1.intersection(set2)) # different methods to invoke the same function
        
    def test_contain_only_immutables(self):
        aset = set()
        
        aset.add(1)
        aset.add("cheka")
        
        # non-hashable object (that is mutable) objects cannot be contained in set
        self.assertRaises(TypeError, lambda : aset.add([]) )
        
if __name__ == "__main__":
    unittest.main()